package com.zzyl.mapper;

import com.github.pagehelper.Page;
import com.zzyl.entity.Reservation;
import com.zzyl.vo.ReservationCountVo;
import com.zzyl.vo.ReservationVo;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.time.LocalDateTime;
import java.util.List;

@Mapper
public interface CustomerReservationMapper {
    /**
     * 查询取消预约次数
     * @param userId
     * @return
     */
    @Select("SELECT COUNT(*) FROM reservation WHERE status = 2 AND create_by = #{userId} GROUP BY create_by;")
    Integer getCancelledCount(Long userId);

    /**
     * 查询每个时间段剩余的预约次数
     * @param startTime
     * @param endTime
     * @return
     */
    @Select("SELECT time, COUNT(*) count FROM reservation WHERE time BETWEEN #{startTime} AND #{endTime} AND status = 0 GROUP BY time;")
    List<ReservationCountVo> getCountByTime(String startTime, String endTime);

    /**
     * 新增预约
     * @param reservation
     */
    @Insert("INSERT INTO reservation VALUES (#{id},#{name},#{mobile},#{time},#{visitor},#{type},#{status},#{createTime},#{updateTime},#{createBy},#{updateBy},#{remark});")
    void save(Reservation reservation);

    /**
     * 分页查询预约
     *
     * @param status
     * @param userId
     * @return
     */
    Page<ReservationVo> list(Integer status, Long userId);

    @Update("UPDATE reservation SET status = 2 WHERE id = #{id}")
    void update(Long id);

    /**
     * 根据手机号查询预约时间
     *
     * @param mobile
     * @param time
     * @return
     */
    @Select("SELECT * FROM reservation WHERE mobile = #{mobile} AND time = #{time} AND status = 0")
    ReservationVo getTimeByMobileAndTime(String mobile, String time);

    /**
     * 查询所有预约
     * @return
     */
    @Select("SELECT * FROM reservation WHERE time < NOW() AND status = 0")
    List<ReservationVo> getExpireTime();

    /**
     * 批量更改状态
     * @param reservationVos
     */
    void updateBatch(List<ReservationVo> reservationVos);
}
